﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;

using SBPweb.PROvibe.Shared.Contracts.Content;

using SBPweb.PROvibe.Server.Framework.Session;

namespace SBPweb.PROvibe.Server.Framework.Content
{

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ContentProvider : IContentProvider
    {

        public virtual int GetCurrentPageId(ClientDescriptor clientDescriptor)
        {
            return 0;
        }

        public virtual int GetCurrentThemeManagerId(ClientDescriptor clientDescriptor)
        {
            return 1;
        }

        public virtual TypeDescriptor GetTypeDescriptorById(int moduleId)
        {
            switch (moduleId)
            {
                default:
                case 0:
                    return new TypeDescriptor(
                        "SBPweb.PROvibe.Client.Modules.Default.xap",
                        "SBPweb.PROvibe.Client.Modules.Default.dll",
                        "SBPweb.PROvibe.Client.Modules.Default.Entry"
                    );
                case 1:
                    return new TypeDescriptor(
                        "SBPweb.PROvibe.Client.Modules.Themes.xap",
                        "SBPweb.PROvibe.Client.Modules.Themes.dll",
                        "SBPweb.PROvibe.Client.Modules.Themes.ThemeManager"
                    );
                case 10:
                    return new TypeDescriptor(
                        "SBPweb.PROvibe.Client.Modules.Controls.xap",
                        "SBPweb.PROvibe.Client.Modules.Controls.dll",
                        "SBPweb.PROvibe.Client.Modules.Controls.Login"
                    );
                case 11:
                    return new TypeDescriptor(
                        "SBPweb.PROvibe.Client.Modules.Controls.xap",
                        "SBPweb.PROvibe.Client.Modules.Controls.dll",
                        "SBPweb.PROvibe.Client.Modules.Controls.Media"
                    );
            }
        }

        public virtual IDictionary<string, object> GetFullSessionBag(ClientDescriptor clientDescriptor)
        {
            IDictionary<string, object> ret = new Dictionary<string, object>();
            foreach (KeyValuePair<string, object> kvp in SessionManager.ReplicateSession(clientDescriptor.SessionId))
            {
                ret.Add(kvp);
            }
            return ret;
        }

        public virtual void PutFullSessionBag(ClientDescriptor clientDescriptor, IDictionary<string, object> values)
        {
            SessionManager s = SessionManager.ReplicateSession(clientDescriptor.SessionId);
            foreach (KeyValuePair<string, object> kvp in values)
            {
                s.Put<object>(kvp.Key, kvp.Value);
            }
        }

        public virtual void AddSessionBagItem(ClientDescriptor clientDescriptor, string key, object value)
        {
            SessionManager s = SessionManager.ReplicateSession(clientDescriptor.SessionId);
            s.Put<object>(key, value);
        }

        public virtual void ChangeSessionBagItem(ClientDescriptor clientDescriptor, string key, object newValue)
        {
            SessionManager s = SessionManager.ReplicateSession(clientDescriptor.SessionId);
            s.Put<object>(key, newValue);
        }

        public virtual void RemoveSessionBagItem(ClientDescriptor clientDescriptor, string key)
        {
            SessionManager s = SessionManager.ReplicateSession(clientDescriptor.SessionId);
            s.Remove<object>(key);
        }

    }

}